import asyncio

import requests as requests
from pyppeteer import launch


async def main():
    # proxy = requests.get('http://proxy.jiayincredit.net/random').text  # 代理服务器地址和端口
    # print(f'proxy:{proxy}')
    # browser = await launch({'args': ['--proxy-server=http://' + proxy], 'headless': False})
    browser = await launch({'headless': False})
    page = await browser.newPage()
    await page.goto('http://www.baidu.com')
    # await page.waitForNavigation()
    await asyncio.sleep(5)

    title = await page.evaluate('document.title')
    print(f'title:{title}')
    # elements = await page.xpath('//*[@id="su"]')
    # 使用 XPath 获取标签中间的值
    element = await page.xpath('//*[@id="hotsearch-refresh-btn"]/span')  # 将 'tag_name' 替换为你要定位的标签名
    value = await page.evaluate('(element) => element.textContent', element[0])
    print(value)



    await browser.close()

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())
